<template>
  <el-container class="home_comtainer" style="font-size:12px;">
    <el-header>
      <div>
        <img src="../assets/logo.png" />
        <span>葳美生活后台管理系统</span>
      </div>
      <el-button type="info" @click="removeLogin()">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="!menuCollapse ? '200px' :'56px'">
        <div class="toggle-btn" @click="collapseMenu">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#3390ff"
          unique-opened
          :collapse="menuCollapse"
          :collapse-transition="false"
          router
          :default-active="navState"
        >
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconsList[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item
              :index="'/'+child.path"
              v-for="child in item.children"
              :key="child.id"
              @click="saveNavState('/'+child.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{child.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      menuList: [],
      iconsList: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      menuCollapse: false,
      // 被激活的菜单地址
      navState: ''
    }
  },
  methods: {
    // 退出
    removeLogin() {
      // 清除token
      window.sessionStorage.clear()
      // 跳转到登录页
      this.$router.push('/login')
    },
    // 左侧菜单列表
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 显示隐藏菜单栏
    collapseMenu() {
      this.menuCollapse = !this.menuCollapse
    },
    // 保存选中子菜单高亮
    saveNavState(name) {
      this.navState = name
      window.sessionStorage.setItem('navState', name)
    }
  },
  created() {
    this.getMenuList()
    this.navState = window.sessionStorage.getItem('navState')
  }
}
</script>
<style lang="less" scoped>
.home_comtainer {
  height: 100%;
}
.el-header {
  background-color: #373d05;
  color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 10px;
  // font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    img {
      width: 45px;
      height: 45px;
      border-radius: 50%;
    }
    span {
      margin-left: 10px;
    }
  }
}

.el-aside {
  background-color: #333744;
  color: #fff;
  .el-menu {
    border: none !important;
  }
}

.el-main {
  background-color: #eaedf1;
  color: #333;
}

.iconfont {
  margin-right: 10px;
}
.toggle-btn {
  background-color: #4a5064;
  font-size: 12px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
// .el-submenu__title {
//   font-size: 12px !important;
// }
// .el-menu-item {
//   font-size: 12px !important;
// }
</style>
